package cn.xyinping.hrm.service.impl;

import cn.xyinping.hrm.constans.BasicConstans;
import cn.xyinping.hrm.doc.CourseDoc;
import cn.xyinping.hrm.domain.Course;
import cn.xyinping.hrm.domain.CourseDetail;
import cn.xyinping.hrm.domain.CourseMarket;
import cn.xyinping.hrm.domain.CourseType;
import cn.xyinping.hrm.dto.CourseAddDto;
import cn.xyinping.hrm.feign.CourseESFeign;
import cn.xyinping.hrm.mapper.CourseMapper;
import cn.xyinping.hrm.service.ICourseDetailService;
import cn.xyinping.hrm.service.ICourseMarketService;
import cn.xyinping.hrm.service.ICourseService;
import cn.xyinping.hrm.service.ICourseTypeService;
import cn.xyinping.hrm.util.AjaxResult;
import cn.xyinping.hrm.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xyinping
 * @since 2021-11-26
 */
@Service
@Transactional
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    /**
     * 课程添加
     * 1. 使用注解校验参数非空 jsr303
     * 2. 课程名称重复判断
          (1)根据名字查询课程
       3.保存课程
       4.保存营销
       5.保存详情
       6.课程分类下课程数加一
     * @param courseAddDto 课程管理添加的参数
     */
    @Autowired
    private ICourseMarketService courseMarketService;
    @Autowired
    private ICourseDetailService courseDetailService;
    @Autowired
    private ICourseTypeService courseTypeService;
    @Autowired
    private CourseESFeign courseESFeign;
    @Override
    public void save(CourseAddDto courseAddDto) {
        Course courseParam = courseAddDto.getCourse();
        CourseDetail courseDetail = courseAddDto.getCourseDetail();
        CourseMarket courseMarket = courseAddDto.getCourseMarket();
        // 根据名字查询课程
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("name", courseParam.getName());
        Course course = super.selectOne(wrapper);
        AssertUtil.isNull(course,"课程名字重复" );
        // 保存课程
        courseParam.setStatus(BasicConstans.course.STATES_OFF_LINE);
        courseParam.setTenantId(26L);
        courseParam.setTenantName("源码时代");
        courseParam.setUserId(42L);
        courseParam.setUserName("yhptest");
        courseParam.setSaleCount(0);
        courseParam.setViewCount(0);
        courseParam.setCommentCount(0);
        super.insert(courseParam);
        // 保存营销
        courseMarket.setId(courseParam.getId());
        courseMarketService.insert(courseMarket);
        // 保存详情
        courseDetail.setId(courseParam.getId());
        courseDetailService.insert(courseDetail);
        // 课程分类下课程数加一
        CourseType courseType = courseTypeService.selectById(courseParam.getCourseTypeId());
        // 切割 [1037，1039，1040]
        String[] typeId= courseType.getPath().split("\\.");
        courseTypeService.addTotalCourseByIds(1,typeId);

    }

    /**
     * 课程上线
     * 1. 根据id查询课程
     * 2. 修改状态为上架要课程状态是下架才能上架
     * 3. 把课程保存到ES中
     * @param id
     */
    @Override
    public void onLineCourse(Long id) {
        // 根据Id查询课程
        Course course = super.selectById(id);
        // 判断课程状态是否是下架
        AssertUtil.isEquals(course.getStatus(),BasicConstans.course.STATES_OFF_LINE,"状态必须为下架");
        // 修改课程状态
        course.setStatus(BasicConstans.course.STATES_IN_LINE);
        course.setOnlineTime(new Date());
        super.updateById(course);
        // 把课程保存到ES中
        // 编写search到feign接口，保存课程
        CourseMarket market = courseMarketService.selectById(id);
        CourseDoc courseDoc = new CourseDoc();
        BeanUtils.copyProperties(market, courseDoc);
        BeanUtils.copyProperties(course, courseDoc);
        // 收费
        courseDoc.setChargeName(market.getCharge()==2?"收费":"免费");

        AjaxResult save = courseESFeign.save(courseDoc);
        AssertUtil.isTrue(save.isSuccess(),"课程发布失败");

    }
}
